﻿@page "/application"

@using Puppy.Scheduler.Applications

@inject IClusterClient client

<Card Margin="Margin.Is4.OnY">
    <CardHeader>
        <CardTitle>应用管理</CardTitle>
    </CardHeader>

    <CardBody>
        <Field Horizontal="true">
            <FieldBody ColumnSize="ColumnSize.IsAuto">
                <Button Color="Color.Primary" Clicked="@OnCreateApp">创建应用</Button>
            </FieldBody>
            <FieldBody ColumnSize="ColumnSize.Is5">
                <TextEdit Placeholder="请输入" @bind-Text="@input"></TextEdit>
            </FieldBody>
            <FieldBody ColumnSize="ColumnSize.Is2">
                <Button Color="Color.Success" Clicked="@Search" Outline="true">查询</Button>
            </FieldBody>
        </Field>
    </CardBody>

    <CardBody>
        <DataGrid TItem="ApplicationEntity"
                  Data="@ApplicationList"
                  Editable="false">
            <DataGridColumns>
                <DataGridColumn Width="150px"
                                TItem="ApplicationEntity"
                                Field="@nameof(ApplicationEntity.Name)"
                                Caption="应用名称">
                </DataGridColumn>
                <DataGridColumn Width="200px"
                                TItem="ApplicationEntity"
                                Field="@nameof(ApplicationEntity.Description)"
                                Caption="描述">
                </DataGridColumn>
                <DataGridColumn Width="200px"
                                TItem="ApplicationEntity"
                                Field="@nameof(ApplicationEntity.MaxTask)"
                                Caption="已有/最大任务数">
                </DataGridColumn>
                <DataGridColumn Width="200px"
                                TItem="ApplicationEntity"
                                Field="@nameof(ApplicationEntity.InstanceNum)"
                                Caption="实例总数">
                </DataGridColumn>
            </DataGridColumns>
        </DataGrid>
    </CardBody>
</Card>

<Modal @ref="modalRef">
    <ModalContent IsCentered="true">
        <ModalHeader>
            <ModalTitle>创建应用</ModalTitle>
            <CloseButton />
        </ModalHeader>
        <ModalBody>
            <Field>
                <FieldLabel>应用名称</FieldLabel>
            </Field>
            <Field>
                <FieldLabel>描述</FieldLabel>
            </Field>
            <Field>
                <FieldLabel>内存使用率</FieldLabel>
            </Field>
            <Field>
                <FieldLabel>磁盘使用率</FieldLabel>
            </Field>
            <Field>
                <FieldLabel>最大任务数</FieldLabel>
            </Field>
        </ModalBody>
        <ModalFooter>
            <Button Color="Color.Primary" Clicked="@HideModal">保存</Button>
            <Button Color="Color.Secondary" Clicked="@HideModal">取消</Button>
        </ModalFooter>
    </ModalContent>
</Modal>

@code {
    string input;
    List<ApplicationEntity> ApplicationList;
    Modal modalRef;

    protected override Task OnInitializedAsync()
    {
        ApplicationList = new List<ApplicationEntity>();

        return base.OnInitializedAsync();
    }

    void OnCreateApp()
    {
        modalRef.Show();
    }

    void HideModal()
    {
        modalRef.Hide();
    }

    void Search()
    {
        Console.WriteLine($"to search key {input}");
    }
}
